﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using CircleCut.Models;
using Common;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CircleCut.Controllers
{
    public class CirclesController : Controller
    {
        private InputDBContext db = new InputDBContext();
        //public static List<Common.Models.Input> In = new List<Common.Models.Input>();
        // public static List<Common.Models.Output> Out = new List<Common.Models.Output>();

        public static List<CircleCut.Models.Circle> CirList = new List<CircleCut.Models.Circle>();

        // GET: Circles
        public  ActionResult Index(string searchNum)
        {
            var CirLst = new List<string>();             
            var CirQry = from d in db.Circles
                             orderby d.Number
                             select d.Number;

            CirLst.AddRange(CirQry.Distinct());
            ViewBag.searchNum = new SelectList(CirLst);

            var Num = from m in db.Circles
                           select m;

              if (!string.IsNullOrEmpty(searchNum))
              {
                  Num = Num.Where(x => x.Number == searchNum);
              }

            CirList = Num.ToList();
            //////////////////////
            /*
            var list = Num.ToList();               
            var i = 0;          
            input.Diameter = new double[input.Num];
            input.Demand = new int[input.Num];
            input.Value = new double[input.Num];
            input.Squer = 0.0;
            for (int j = 0; j < list.Count; j++)
            {
                //毛胚规格及数量
                var k = list[i++];
                var l = k.Diameter;
                var m = k.Demand;
                input.Diameter[j] = double.Parse(l);
                input.Demand[j] = int.Parse(m);
                input.Value[j] = 1.0;
                input.Squer += Math.PI * Math.Pow(input.Diameter[j] / 2, 2) * input.Demand[j];
            }         
            */

            return View(Num);
                      
        }

        // GET: Circles/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Circle circle = db.Circles.Find(id);
            if (circle == null)
            {
                return HttpNotFound();
            }
            return View(circle);
        }

        // GET: Circles/Create
        public ActionResult Create()
        {            
           return View();
        }

        // POST: Circles/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(string S,[Bind(Include = "ID,Number,Diameter,Demand")] Circle circle)
        {
            var a = S;


            if (ModelState.IsValid)
            {
                db.Circles.Add(circle);
                db.SaveChanges();
                //return RedirectToAction("Index");
            }

            return View(circle);
        }

        // GET: Circles/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Circle circle = db.Circles.Find(id);
            if (circle == null)
            {
                return HttpNotFound();
            }
            return View(circle);
        }

        // POST: Circles/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Number,Diameter,Demand")] Circle circle)
        {
            if (ModelState.IsValid)
            {
                db.Entry(circle).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(circle);
        }

        // GET: Circles/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Circle circle = db.Circles.Find(id);
            if (circle == null)
            {
                return HttpNotFound();
            }
            return View(circle);
        }

        // POST: Circles/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Circle circle = db.Circles.Find(id);
            db.Circles.Remove(circle);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
